package com.fy.proj5_permission_transaction_cache.controller;

import com.fy.proj5_permission_transaction_cache.model.Role;
import com.fy.proj5_permission_transaction_cache.service.RoleService;
import com.fy.proj5_permission_transaction_cache.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

// 简化版角色管理控制器
@RestController
@RequestMapping("/api/roles")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 获取所有系统角色（简化版）
     */
    @GetMapping
    public Map<String, Object> getAllRoles() {
        try {
            List<Role> roles = roleService.findAllRoles();
            return ResponseUtil.success(roles);
        } catch (Exception e) {
            return ResponseUtil.error(500, "获取角色列表失败: " + e.getMessage());
        }
    }

    /**
     * 获取用户的角色（为用户管理页面提供数据）
     */
    @GetMapping("/user/{userId}")
    public Map<String, Object> getRolesByUserId(@PathVariable Long userId) {
        try {
            List<Role> roles = roleService.findRolesByUserId(userId);
            return ResponseUtil.success(roles);
        } catch (Exception e) {
            return ResponseUtil.error(500, "获取用户角色失败: " + e.getMessage());
        }
    }
}